import {
    OrderDetail
} from './order-detail.model'
import {
    Member
} from './../member/member.model'
import Toast from '@vant/weapp/toast/toast'
const orderDetail = new OrderDetail()
const member = new Member()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        order: '',
        currentId: '',
        currentPrice: '',
        refundModel: false,
        cancelModel: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this._loadData(options.id)
    },

    /**
     * 获取所有数据
     * @param {*} id
     */
    _loadData(id) {
        let that = this
        that.getOrderData(id)
    },

    /**
     * 获取订单详情
     * @param {*} id
     */
    getOrderData(id) {
        let that = this
        orderDetail.getOrderData(id).then((res) => {
            that.setData({
                order: res.data
            })
        })
    },

    /**
     * 支付
     * @param {*} options
     */
    doPay(options) {
        let order_id = orderDetail.getDataSet(options, 'order_id'),
            order_sn = orderDetail.getDataSet(options, 'order_sn'),
            total_amount = orderDetail.getDataSet(options, 'total_amount'),
            subscribe_id = orderDetail.getDataSet(options, 'subscribe_id'),
            orderData = {
                order_id: order_id,
                order_sn: order_sn,
                total_amount: total_amount,
                subscribe_id: subscribe_id
            }
        orderDetail.placeOrder(orderData).then((res) => {
            orderDetail.pay(res).then(() => {
                member.getMemberData().then((result) => {
                    let userInfo = orderDetail.getStorage('userInfo')
                    userInfo.member = result.data.member
                    orderDetail.setStorage('userInfo', userInfo)
                }).then(() => {
                    Toast({
                        type: 'success',
                        message: '支付成功',
                        onClose: () => {
                            wx.reLaunch({
                                url: '/pages/order/order?status=2&page=1'
                            })
                        },
                    })
                })
            }).catch(() => {
                Toast.fail('支付失败')
            })
        })
    },

    /**
     * 取消订单
     * @param {*} options
     */
    onConfirmCancel(options) {
        let that = this,
            id = orderDetail.getDataSet(options, 'id')
        orderDetail.cancelOrder(id).then(() => {
            that.setData({
                cancelModel: false
            })
        }).then(() => {
            Toast({
                type: 'success',
                message: '取消成功',
                onClose: () => {
                    wx.reLaunch({
                        url: '/pages/order/order?status=4&page=1&searchedTitle='
                    })
                },
            })
        })
    },

    /**
     * 退款
     */
    onConfirmRefund() {
        let that = this
        orderDetail.refundOrder(that.data.currentId).then(() => {
            that.setData({
                refundModel: false
            })
        }).then(() => {
            wx.navigateTo({
                url: `/pages/refund/refund?price=${that.data.currentPrice}`,
            })
        })
    },

    /**
     * 打开取消订单窗口
     * @param {*} options
     */
    onOpenCancelModel(options) {
        let that = this,
            id = orderDetail.getDataSet(options, 'id')
        that.setData({
            currentId: id,
            cancelModel: true,
        });
    },

    /**
     * 打开退款窗口
     * @param {*} options
     */
    onOpenRefundModel(options) {
        let that = this,
            id = orderDetail.getDataSet(options, 'id'),
            price = orderDetail.getDataSet(options, 'price')
        that.setData({
            currentId: id,
            currentPrice: price,
            refundModel: true,
        });
    },

    /**
     * 关闭申请窗口
     */
    onCloseModel() {
        let that = this
        that.setData({
            cancelModel: false,
            refundModel: false
        });
    },

    /**
     * 跳转到评论列表
     * @param {*} options
     */
    onJumpCommentList() {
        wx.navigateTo({
            url: `/pages/comment/comment`,
        })
    },

    /**
     * 跳转到评论
     * @param {*} options
     */
    onJumpCommentWrite(options) {
        let id = orderDetail.getDataSet(options, 'id')
        wx.navigateTo({
            url: `/pages/comment-write/comment-write?id=${id}`,
        })
    }
})